<?php
/**
 * Created by IntelliJ IDEA.
 * User: dhy
 * Date: 2015/9/14
 * Time: 9:52
 */
namespace App\Http\Controllers\Admin\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;

use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use App\Http\Models\Admin\AdminUsers;


class AuthController extends Controller{

    public function loginout(){
        return Redirect::route("admin.auth.login");
    }

    /**
     * 显示登录页面
     * @return View
     */
    public function getLogin()
    {
        return View::make('admin.auth.login');
    }

    /**
     * 退出登录
     * @param Request $request
     * @return mixed
     */
    public function getLogout(Request $request)
    {
        $request->session()->forget('adminUser');
        return  Redirect::route("admin.login");
    }

    public function postLogin(Request $request){

        $username = $request->input('username');
        $password = $request->input('password');

        $validator = Validator::make(Input::all(),
            [
            'username' => 'required',
            'password' => 'required',
        ]);


        if($validator->fails()){
            return View::make("admin.auth.login")->withMessage($validator->errors()->first())->withUsername($username);
        }else{
            $user = AdminUsers::where('user_name',$username)->where("password",md5($password))->first();
            if($user){
                $user->photo_url = url($user->photo_url);
                $request->session()->put('adminUser',$user);
                return Redirect::route("admin.home.index");
            }else{
                return View::make("admin.auth.login")->withMessage(trans('admin/message.loginfail') )->withUsername($username);
            }

        }
    }

}